<?php
/**
 * Created by PhpStorm.
 * User: Sunqj
 * Date: 2017/1/11
 * Time: 下午9:28
 */
namespace app\admin\controller;

use think\Controller;
use think\Image;
use think\Db;
use app\common\model\Storage;

class Utils extends Controller
{
    protected function _initialize()
    {
        if (!session("?H_ADMIN_LOGIN_USER") || session("H_ADMIN_DUE_TIME") < time())
        {
            session("H_ADMIN_REFERER", url("Index/index"));
//            if (strtolower(request()->controller()) === "login")
//            {
//                session("H_ADMIN_REFERER", url("Index/index"));
//            }
//            else
//            {
//                $controller = request()->controller();
//
//                session("H_ADMIN_REFERER", url($controller . "/index"));
//            }

            //判断是否为ajax操作
            if (request()->isAjax())
            {
                die(json_encode(array("status"=>-1,"url"=>url("Login/index"))));
            }
            else
            {
                die("<script>if (window.parent){window.parent.location.href='"
                    . url("Login/index") . "';}else{location.href='"
                    . url("Login/index") . "';}</script>");
            }
        }
    }
    public function home()
    {
        return $this->fetch();
    }

    public function uploader()
    {
        if (request()->isAjax())
        {
            $this->view->engine->layout("public/modalLayout");

            //获取图片元素ID
            $imageId = request()->param("element_id");
            $this->assign("image_id", $imageId);

            $tpl = $this->fetch();

            die(json_encode(array("status"=>0,"html"=>$tpl)));
        }
        else
        {
            return "";
        }
    }
    //清除系统缓存
    public function clearCache()
    {
        //清除缓存
        clear_cache();

        //清除缓存成功
        $this->success('清除缓存成功');
    }

    //获取地区
    public function queryRegion()
    {
        //仅支持AJAX操作
        if (request()->isAjax())
        {
            //上级地区ID
            $parentId = request()->param("parent_id");

            //获取地区列表
            $list = Db::name("region")
                ->where("parent_id", $parentId)
                ->where('is_visible',1)
                ->order("order_num desc")
                ->field("id,title")
                ->select();

            return $list;
        }
    }

    //获取分类
    public function queryCategory()
    {
        //仅支持AJAX操作
        if (request()->isAjax())
        {
            //上级地区ID
            $parentId = request()->param("parent_id");

            //获取地区列表
            $list = Db::name("category")
                ->where("parent_id", $parentId)
                ->where('is_valid',1)
                ->order("display_order DESC,id DESC")
                ->field("id,title")
                ->select();

            return $list;
        }
    }

    public function queryBusCategory(){
        //仅支持AJAX操作
        if (request()->isAjax())
        {
            //获取分类列表
            $list = Db::name("category")
                ->alias('ca')
                ->where('ca.is_valid',1)
                ->where('ca.level',1)
                ->order("ca.display_order DESC,ca.id DESC")
                ->field("ca.id,ca.title")
                ->select();

            return $list;
        }
    }

    public function queryBrandCategory(){
        //仅支持AJAX操作
        if (request()->isAjax())
        {
            //上级地区ID
            $category_id1 = request()->param("category_id1");

            //获取地区列表
            $list = Db::name("brand")
                ->alias('br')
                ->join('__CATEGORY_BRAND__ cb','cb.category_id1='.$category_id1.' AND br.id=cb.brand_id')
                ->where('br.is_valid',1)
                ->order("br.display_order DESC,br.id DESC")
                ->field("br.id,br.title")
                ->select();

            return $list;
        }
    }


    public function queryUser(){
        if (request()->isAjax())
        {
            $mobile = request()->param("mobile");
            $id = Db::name("user")
                ->where("mobile", $mobile)
                ->value('id');
            return $id;
        }
    }

//    public function queryBus(){
//        if (request()->isAjax())
//        {
//            $username = request()->param("username");
//            $id = Db::name("business")
//                ->where("username", $username)
//                ->value('id');
//            return $id;
//        }
//    }

    //上传单图片
    public function uploadSingleImage()
    {
        $storage = new Storage();
        //bootstrap-fileinput
        if (request()->isAjax())
        {
            //获取上传图片的名称
            $imageName = request()->param("name");
            $result = $storage->upload($imageName,['ext'=>config('img_ext')]);

            if ($result['status']==0)
            {
                return array("status"=>0,"url"=>$result['data']);
            }
            else
            {
                return array("error"=>$result['info']);
            }
        }
    }

    public function uploadSingleVideo()
    {
        $storage = new Storage();
        //bootstrap-fileinput
        if (request()->isAjax())
        {
            //获取上传图片的名称
            $imageName = request()->param("name");

            $result = $storage->upload($imageName,['ext'=>config('videos_ext')]);

            if ($result['status']==0)
            {
                return array("status"=>0,"url"=>$result['data']);
            }
            else
            {
                return array("error"=>$result['info']);
            }
        }
    }



    //删除单张图片
    public function removeSingleImage()
    {
        if (request()->isAjax())
        {

        }
    }

    //验证手机号是否已被使用
    public function isUsedMobile()
    {
        if (request()->isAjax())
        {
            //手机号
            $mobile = request()->param("tel");
            //用户ID
            $userId = request()->param("uid");

            //判断手机号是否已被使用
            $count = Db::name("user")
                ->where("is_valid", 1)
                ->where("id", "NEQ", $userId)
                ->where("tel", $mobile)
                ->count();

            if ($count)
            {
                die("false");
            }
            else
            {
                die("true");
            }
        }
    }

    //获取商会
    public function queryBuss()
    {
        //仅支持AJAX操作
        if (request()->isAjax())
        {
            //名称或id
            $type = request()->param('type');
            $title = request()->param("title");
            if($type==1){
                $where['title'] = ['like',"%".$title."%"];
            }else{
                $where['id'] = $title;
            }


            //获取商家
            $list = Db::name("business")
                ->where($where)
                ->where('is_valid',1)
                ->where('is_pass',1)
                ->where('is_frozen',0)
                ->order("display_order desc,update_time desc")
                ->field("id,title")
                ->select();

            return $list;
        }
    }

    public function queryBusPresell(){
        //仅支持AJAX操作
        if (request()->isAjax())
        {
            //名称或id
            $business_id = request()->param('business_id');

            //获取商家
            $arr = Db::name("business")
                ->where('id',$business_id)
                ->where('is_valid',1)
                ->field('can_presell,can_group')
                ->find();

            return $arr;
        }
    }

    public function queryBusTag(){
        //仅支持AJAX操作
        if (request()->isAjax())
        {
            //名称或id
            $business_id = request()->param('business_id');
            $goods_id = intval(request()->param('goods_id'));

            if(!empty($goods_id)){
                //获取商家
                $list = Db::name("business_tag")
                    ->alias('bt')
                    ->join('__BUSINESS_TAG_GOODS__ bg','bg.goods_id='.$goods_id.' AND bt.id=bg.business_tag_id','LEFT')
                    ->where('bt.business_id',$business_id)
                    ->where('bt.is_pass',1)
                    ->order('bt.display_order DESC,bt.id DESC')
                    ->field('bt.id,bt.title,bt.is_visible,bg.id as bgid')
                    ->select();
            }else{
                //获取商家
                $list = Db::name("business_tag")
                    ->where('business_id',$business_id)
                    ->where('is_pass',1)
                    ->order('display_order DESC,id DESC')
                    ->field('id,title,is_visible')
                    ->select();
            }


            foreach ($list as $key=>$item){

                $list[$key]['title'] = get_visible_title($item['title'],$item['is_visible']).'【ID：'.$item['id'].'】';
            }
            return $list;
        }
    }

    //验证身份证号
    public function validateIdentity()
    {
        if (request()->isAjax())
        {
            //身份证号
            $identity = request()->param("identity");

            if (empty($identity) || validate_identity($identity))
            {
                die("true");
            }
            else
            {
                die("false");
            }
        }
    }

}